package com.headline.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.headline.common.Common;
import com.headline.common.DateHelper;
import com.headline.model.*;
import com.headline.service.SpiderNewsInfoService;
import com.headline.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

@Controller
@RequestMapping(value = "/api")
public class ApiController extends BaseController {
    //    @Autowired
//    UserRepository userRepository;
    UserService userService = new UserService();
    SpiderNewsInfoService spiderNewsInfoService = new SpiderNewsInfoService();


    @ResponseBody
    @RequestMapping(value = "user/list", method = RequestMethod.GET)//produces = "application/json; charset=utf-8"
    public String getUsers() {
        List<UserEntity> list = userService.queryList("");
        Gson gson = new Gson();
        JsonResult js = new JsonResult(list);
        String result = gson.toJson(js);
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "news/list", method = RequestMethod.GET)
    public String getNews() {
        List<RecommondArticleResult> list = spiderNewsInfoService.queryApp(getMax_id(), getSince_id());
        ArticleResult ar = new ArticleResult();
        ar.setItems(list);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        if (list != null && list.size() > 0) {
            ArticleNext an = new ArticleNext();
            java.util.Date d = list.get(list.size() - 1).getRefresh_time();
            an.setMax_time(DateHelper.dateToTimeSpan(d.toString()));
            ar.setMax_time(an);
        }
        JsonResult js = new JsonResult(ar);
        if (list.size() == 0) {
            js.setMessage("暂无新内容，先休息一下");
        }
        String result = gson.toJson(js);
        return result;
    }


}
